package com.example.demo.task;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author: binbin
 * @createDate: 2022-08-22
 */
@Service
public class TaskService {

    @Autowired
    public Config config;

    public String doTask() {

        //获取access_token
        String getOpenIdUrl = config.getTokenUrl().replace("APPID", config.getAppId()).replace("APPSECRET", config.getAppSecret());
        String response = HttpClientUtil.doGet(getOpenIdUrl);
        JSONObject jsonObject = JSONObject.parseObject(response);
        String accessToken = jsonObject.get("access_token") == null ? null : jsonObject.get("access_token").toString();
        String url = config.getUrl().replace("ACCESS_TOKEN", accessToken);
        return HttpClientUtil.doPostJson(url, noticeTemplate());
    }

    /**
     * @return
     */
    public JSONObject weather() {
        //{"msg":"success","log_id":420515492273795072,"code":200,"data":{"sunrise":"05:32","city":"北京","air":"12","update_time":"2022-08-23 09:30:00","rain_24h":"0","province":"北京","hour":[{"temp":"21","wea":"阴","wea_code":"yin","time":"2022-08-23 08:00:00","wind_level":"<3级","wind":"北风"},{"temp":"22","wea":"多云","wea_code":"duoyun","time":"2022-08-23 09:00:00","wind_level":"<3级","wind":"西南风"},{"temp":"23","wea":"多云","wea_code":"duoyun","time":"2022-08-23 10:00:00","wind_level":"<3级","wind":"东南风"},{"temp":"25","wea":"晴","wea_code":"qing","time":"2022-08-23 11:00:00","wind_level":"<3级","wind":"北风"},{"temp":"26","wea":"晴","wea_code":"qing","time":"2022-08-23 12:00:00","wind_level":"<3级","wind":"东南风"},{"temp":"27","wea":"晴","wea_code":"qing","time":"2022-08-23 13:00:00","wind_level":"<3级","wind":"西南风"},{"temp":"28","wea":"晴","wea_code":"qing","time":"2022-08-23 14:00:00","wind_level":"<3级","wind":"北风"},{"temp":"28","wea":"多云","wea_code":"duoyun","time":"2022-08-23 15:00:00","wind_level":"<3级","wind":"北风"},{"temp":"28","wea":"多云","wea_code":"duoyun","time":"2022-08-23 16:00:00","wind_level":"<3级","wind":"北风"},{"temp":"28","wea":"多云","wea_code":"duoyun","time":"2022-08-23 17:00:00","wind_level":"<3级","wind":"北风"},{"temp":"26","wea":"多云","wea_code":"duoyun","time":"2022-08-23 18:00:00","wind_level":"<3级","wind":"西北风"},{"temp":"26","wea":"多云","wea_code":"duoyun","time":"2022-08-23 19:00:00","wind_level":"<3级","wind":"西北风"},{"temp":"26","wea":"多云","wea_code":"duoyun","time":"2022-08-23 20:00:00","wind_level":"<3级","wind":"西北风"},{"temp":"23","wea":"多云","wea_code":"duoyun","time":"2022-08-23 21:00:00","wind_level":"<3级","wind":"西风"},{"temp":"23","wea":"多云","wea_code":"duoyun","time":"2022-08-23 22:00:00","wind_level":"<3级","wind":"西风"},{"temp":"22","wea":"多云","wea_code":"duoyun","time":"2022-08-23 23:00:00","wind_level":"<3级","wind":"西南风"},{"temp":"21","wea":"多云","wea_code":"duoyun","time":"2022-08-24 00:00:00","wind_level":"<3级","wind":"西南风"},{"temp":"21","wea":"多云","wea_code":"duoyun","time":"2022-08-24 01:00:00","wind_level":"<3级","wind":"西风"},{"temp":"20","wea":"多云","wea_code":"duoyun","time":"2022-08-24 02:00:00","wind_level":"<3级","wind":"西北风"},{"temp":"20","wea":"多云","wea_code":"duoyun","time":"2022-08-24 03:00:00","wind_level":"<3级","wind":"西风"},{"temp":"20","wea":"多云","wea_code":"duoyun","time":"2022-08-24 04:00:00","wind_level":"<3级","wind":"西风"},{"temp":"20","wea":"阴","wea_code":"yin","time":"2022-08-24 05:00:00","wind_level":"<3级","wind":"西南风"},{"temp":"20","wea":"阴","wea_code":"yin","time":"2022-08-24 06:00:00","wind_level":"<3级","wind":"西南风"},{"temp":"21","wea":"阴","wea_code":"yin","time":"2022-08-24 07:00:00","wind_level":"<3级","wind":"西南风"}],"min_temp":"20","tail_number":"3和8","wind_scale":"6km/h","weather":"多云","alarm":[],"humidity":"32%","wind_speed":"2级","max_temp":"27","rain":"0","temp":"25","visibility":"30km","province_en":"beijing","index":{"yundong":{"level":"适宜","name":"运动指数","content":"预计今日整体气象条件尚可，大部分人可以外出锻炼，但敏感人群要注意天气变化，提前做好调整。"},"xiche":{"level":"适宜","name":"洗车指数","content":"预计今日适宜洗车，未来持续两天无雨天气较好，适合擦洗汽车，晴日将伴您的车子连日洁净。"},"huazhuang":{"level":"暂无","name":"化妆指数","content":""},"chuangyi":{"level":"薄T","name":"穿衣指数","content":"预计今日天气炎热，建议穿着清凉透气的衣物。推荐穿轻棉织物制作的薄T、短裙、短裤等。"},"ganmao":{"level":"高发","name":"感冒指数","content":"感冒高发期，尽量避免外出，外出戴口罩防护。"},"ziwaixian":{"level":"最弱","name":"紫外线强度指数","content":"属弱紫外线辐射天气，无需特别防护。若长期在户外，建议涂擦SPF 在8-12 之间的防晒护肤品。"}},"pressure":"1012","city_en":"beijing","air_pm25":"4","sunset":"19:02","aqi":{"no2":"19","air_level":"优","air_tips":"空气好，除极少数对污染物特别敏感的人群以外，对公众没有危害！","pm25":"4","o3":"37","so2":"3","pm10":"12","air":"12","co":"0.3"},"weather_code":"duoyun","city_id":"101010100","wind":"北风"},"time":1661218706}
        String weatherUrl = config.getWeatherUrl();
        HashMap<String, String> params = new HashMap<>();
        params.put("token", config.getToken());
        params.put("location", "杭州");
        String response = HttpClientUtil.doGet(weatherUrl, params);
        return JSONObject.parseObject(response);
    }

    public JSONObject qingHua() {
        //{"msg":"success","log_id":420514987950682112,"code":200,"data":{"content":"我确实真诚地喜欢过你，想过带你去看每年故宫的初雪，阿拉斯加的海岸线，我曾愿意与你两人独占一江秋，愿意与你郡亭枕上看潮头，铺着红地毯的礼堂，暮霭沉沉的原野，我都曾愿与你共享，我想象过和你一起生活，直到白发苍苍垂垂老矣，同枕共穴，至死不休。"},"time":1661218585}
        String weatherUrl = config.getQingHua();
        HashMap<String, String> params = new HashMap<>();
        params.put("token", config.getToken());
        String response = HttpClientUtil.doGet(weatherUrl, params);
        return JSONObject.parseObject(response);
    }

    private Long getBetween() {
        String dateStr1 = "2021-05-25";
        Date date01 = DateUtil.parse(dateStr1);
        return DateUtil.between(date01, new Date(), DateUnit.DAY);
    }

    private Long getEnd() {
        String dateStr1 = "2023-08-27";
        Date date01 = DateUtil.parse(dateStr1);
        return DateUtil.between(date01, new Date(), DateUnit.DAY);
    }

    public String noticeTemplate() {
        JSONObject weatherResult = weather();
        JSONObject qingHuaResult = qingHua();
        TreeMap<String, TreeMap<String, String>> params = new TreeMap<>();
        //根据具体模板参数组装
        params.put("first", WxTemplate.item("你好", "#000000"));
        params.put("keyword1", WxTemplate.item(JSONObject.parseObject(JSONObject.parseObject(weatherResult.get("data").toString()).get("now").toString()).get("cond_txt").toString(), "#000000"));
        params.put("keyword2", WxTemplate.item(JSONObject.parseObject(JSONObject.parseObject(weatherResult.get("data").toString()).get("now").toString()).get("tmp").toString(), "#000000"));
        params.put("keyword3", WxTemplate.item(String.valueOf(getBetween()), "#000000"));
        params.put("keyword4", WxTemplate.item(String.valueOf(getEnd()), "#000000"));
        params.put("remark", WxTemplate.item(JSONObject.parseObject(qingHuaResult.get("data").toString()).get("content").toString(), "#000000"));
        WxTemplate wxTemplateMsg = new WxTemplate();
        // 模版ID
        wxTemplateMsg.setTemplate_id(config.getTemplate());
        // openId
        wxTemplateMsg.setTouser(config.getToUser());
        // 关键字赋值
        wxTemplateMsg.setData(params);
        return JSONObject.toJSONString(wxTemplateMsg);
    }

}
